package com.hongfeng.support.common;

import java.util.ArrayList;
import java.util.List;

public class Page<T> {
	private int pageSize = Constants.DEFAULT_PAGE_SIZE; // 每页的记录数
	private int pageIndex; // 当前页
	private List<T> data; // 页内数据
	private int total; // 总记录数
	private int indexNumber = Constants.DEFAULT_INDEX_NUMBER;
	private int pageCount;// 总计页数

	public Page() {
		this(Constants.DEFAULT_PAGE_SIZE, 0, new ArrayList<T>(),
				Constants.DEFAULT_INDEX_NUMBER, 1);
	}

	/**
	 * 
	 * 创建一个新的实例 Page.
	 * 
	 */
	public Page(Integer pageIndex, Integer pageSize) {
		this.pageIndex = pageIndex != null ? pageIndex.intValue() : 0;
		this.pageSize = pageSize != null ? pageSize.intValue()
				: Constants.DEFAULT_PAGE_SIZE;
	}

	/**
	 * 
	 * 创建一个新的实例 Page.
	 * 
	 */
	public Page(int pageIndex, int pageSize) {
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
	}

	/**
	 * 
	 * @param pageSize
	 * @param recordCount
	 * @param data
	 * @param pageIndex
	 */
	public Page(int pageSize, int recordCount, List<T> data, int pageIndex) {
		this(pageSize, recordCount, data, Constants.DEFAULT_INDEX_NUMBER,
				pageIndex);
	}

	/**
	 * @param pageSize
	 * @param total
	 * @param data
	 * @param indexNumber
	 * @param pageIndex
	 */
	public Page(int pageSize, int total, List<T> data, int indexNumber,
			int pageIndex) {
		super();
		this.pageSize = pageSize < 1 ? 1 : pageSize;
		this.data = data;
		this.total = total < 0 ? 0 : total;
		this.indexNumber = indexNumber < 0 ? 0 : indexNumber;
		this.pageIndex = pageIndex < 1 ? 0
				: pageIndex > getPageCount() ? getPageCount() : pageIndex;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize
	 *            the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize < 1 ? 1 : pageSize;
	}

	/**
	 * @return the pageNumber
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex
	 *            the pageNumber to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the results
	 */
	public List<T> getData() {
		return data;
	}

	/**
	 * @param results
	 *            the results to set
	 */
	public void setData(List<T> data) {
		this.data = data;
	}

	/**
	 * @return the recordCount
	 */
	public int getTotal() {
		return total;
	}

	/**
	 * @param total
	 *            the recordCount to set
	 */
	public void setTotal(int total) {
		this.total = total < 0 ? 0 : total;
	}

	/**
	 * @return the indexNumber
	 */
	public int getIndexNumber() {
		return indexNumber;
	}

	/**
	 * @param indexNumber
	 *            the indexNumber to set
	 */
	public void setIndexNumber(int indexNumber) {
		this.indexNumber = indexNumber;
	}

	public boolean hasPreviousPage() {
		return pageIndex > 1;
	}

	public boolean hasNextPage() {
		return pageIndex < getPageCount();
	}

	/**
	 * @return the pageCount
	 */
	public int getPageCount() {
		if (total % pageSize == 0) {
			pageCount = total / pageSize;
		} else {
			pageCount = total / pageSize + 1;
		}
		return pageCount;
	}

	public int getStartRow() {
		// 前提是必须先有total的值
		pageIndex = pageIndex < 1 ? 0
				: pageIndex > getPageCount() ? getPageCount() : pageIndex;
		return pageIndex <= 0 ? 0 : pageIndex * (pageSize < 1 ? 1 : pageSize);
	}
}
